package com.tritrees.easygridview.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;





import com.winway.sixbook.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;


public class EasyGridView extends MyGridView implements
		android.widget.AdapterView.OnItemClickListener
{

	public static final String IMAGE = "icon";
	public static final String TEXT = "text";
	
	/** "+"号图片 */
	protected int addResourceID;
	protected Bitmap bmp_add;
	public void setAddImage(int addResourceID)
	{
		this.addResourceID = addResourceID;
	}
	/** "-"号图片 */
	protected int delResourceID;
	protected Bitmap bmp_del;
	public void setDelImage(int delResourceID)
	{
		this.delResourceID = delResourceID;
	}
	
	
	/** 提供给单一的gridview控件用的回调接口 */
	public interface IHandleGridView
	{
		void addImage();

		void delImage(int position);

		void queryImage(int position);
	}
	
	/** 提供给使用多个gridview控件用的回调接口,例如作为listview的子项时 */
	public interface IHandleGridViewWithChild
	{
		void addImage(int childPosition);

		void delImage(int childPosition, int position);

		void queryImage(int childPosition, int position);
	}

	protected IHandleGridView handleGridView;

	public void setHandleGridView(IHandleGridView handleGridView)
	{
		this.handleGridView = handleGridView;
	}
	
	/** 当作为子项时，在父控件里面对应的位置 */
	protected int childPosition;
	protected IHandleGridViewWithChild handleGridViewWithChild;

	public void setHandleGridViewWithChild(int childPosition,
			IHandleGridViewWithChild handleGridViewWithChild)
	{
		this.childPosition = childPosition;
		this.handleGridViewWithChild = handleGridViewWithChild;
	}

	protected boolean isRemoved = false;// 是否为删除状态
	protected boolean isChildItem = false;// 是否作为其他控件的子项
	protected int blankItemCount = 0;
	protected int itemCountInRow = 5;// mygridview每行的子项总数,默认为5
	protected ImageAdapter adapter;
	protected Context context;
	
	protected float imageWidth,imageHeight;//图片的宽高

	protected List<HashMap<String, Object>> dataList;// 用来存放动态数据的数据容器
	protected ArrayList<HashMap<String, Object>> data;// 设置数据资源

	public ArrayList<HashMap<String, Object>> getData()
	{
		return data;
	}

	public void setData(ArrayList<HashMap<String, Object>> data)
	{
		this.data = data;
	}

	public EasyGridView(Context context)
	{
		super(context);
	}

	public EasyGridView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		this.context = context;
		

		this.imageWidth = 100;
		this.imageHeight = 100;
		//2.2版本里 ，几列这个属性在xml里面可以设置，在代码里居然找不到动态获取的方法，这让我很无解，只能自定义一个属性来控制了
		//所以当xml里的numcolumn值修改了，columncount值也要设置为一样
		this.itemCountInRow = 3;
	
		
		if(addResourceID != 0)
		{
			bmp_add = BitmapFactory.decodeResource(getResources(),
					addResourceID);
		}else
		{
			bmp_add = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_add);
		}
		
		if(delResourceID != 0)
		{
			bmp_del = BitmapFactory.decodeResource(getResources(),
					delResourceID);
		}else
		{
			bmp_del = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_del);
		}
	}

	public EasyGridView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}

	/**
	 * 初始化图片和文字数据
	 * 
	 * @param isChildItem 是否作为其他控件的子项
	 * @param data 自定义的数据结构，必须是ArrayList<HashMap<String, Object>>这种数据结构
	 * @param adapter 自定义的适配器， 若要定义自己的布局文件，可以继承这个ImageAdapter,覆盖getview方法
	 */
	public void initData(boolean isChildItem,
			ArrayList<HashMap<String, Object>> data,ImageAdapter adapter)
	{
		this.isChildItem = isChildItem;
		this.data = data;
		this.adapter = adapter;

		dataList = new ArrayList<HashMap<String, Object>>();
		initAdapterData();

		adapter.setData(dataList);
		adapter.setBlankItemCount(blankItemCount);
		adapter.setImageWidth(imageWidth);
		adapter.setImageHeight(imageHeight);

		setOnItemClickListener(this);
		setSelector(new ColorDrawable(Color.TRANSPARENT));
		setAdapter(adapter);
	}

	/**
	 * 这个方法时关键
	 */
	protected void initAdapterData()
	{
		dataList = data;
	}

	/**
	 * 计算空白区域的个数
	 * 
	 * @param num
	 */
	protected void addBlankItem(int num)
	{
		Bitmap bmp_blank = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_blank);
		HashMap<String, Object> map;
		// 当删除状态下时，若最后一个图标是该行的最后一个，则需要有一行的空白区域
		if (isRemoved)
		{
			blankItemCount = (num / itemCountInRow + 1) * itemCountInRow - num;
		} else
		{
			blankItemCount = (num % itemCountInRow == 0 ? num / itemCountInRow
					: (num / itemCountInRow + 1)) * itemCountInRow - num;
		}
		for (int i = 0; i < blankItemCount; i++)
		{
			map = new HashMap<String, Object>();
			map.put(IMAGE, bmp_blank);
			dataList.add(map);
		}
	}

	/**
	 * 更新适配器
	 */
	public void updateAdapter()
	{
		initAdapterData();
		adapter.setBlankItemCount(blankItemCount);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		Log.v("ddddddddddd", "计数器");
		if (isChildItem)
		{
			if (handleGridViewWithChild != null)
			{
				handleGridViewWithChild.queryImage(childPosition,
						position);
			}
		} else
		{
			if (handleGridView != null)
			{
				handleGridView.queryImage(position);
			}
		}
	}

}
